/**
 * 
 */
package com.uit.project.bustrip.bo;

import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;
import android.content.Context;
import android.util.Log;
import com.uit.project.bustrip.appconfig.AppsConfig;
import com.uit.project.bustrip.bointerface.CustomerBO;
import com.uit.project.bustrip.dao.CustomerDaoImpl;
import com.uit.project.bustrip.daointerface.CustomerDao;
import com.uit.project.bustrip.entity.Customer;
import com.uit.project.bustrip.ultil.JSONUltil;
import com.uit.project.bustrip.valueobject.JSONResult;
import com.uit.project.bustrip.valueobject.LoginInfo;

/**
 * @author 
 *
 */
public class CustomerBOImpl extends BaseBOImpl<Customer, Integer> implements CustomerBO{
	public CustomerBOImpl(){
		super();
	}
	public CustomerBOImpl(Context context){
		super(context);
	}
	@Override
	public Customer getInstance() {
		List<Customer> customer = ((CustomerDao)this.getDao()).findAll();
		if(customer.size() > 0)
			return customer.get(0);
		return null;
	}
	private String getLoginURL(String phone, String pass) {
		return AppsConfig.getServiceURL() + "?_module=Customer&_action=auth&_view=json"
				+ "&phone=" + phone + "&pass=" + pass; 
	}
	@Override
	public LoginInfo loginAsync(String phone, String pass) {
		String url = getLoginURL(phone, pass);
		JSONObject result = JSONUltil.getJSONObjectFromURL(url);
		if(result != null){
			LoginInfo loginInfo = null;
			try {
				JSONObject loginResult = result.getJSONObject(AppsConfig.JSON_OBJECT);
				if(loginResult != null){
					loginInfo = getLoginInfo(loginResult);
					return loginInfo;
				}	
				else{
					return null;
				}
			} catch (JSONException e) {
				return null;
			}
		}
		return null;
	}
	public LoginInfo getLoginInfo(JSONObject json){
		LoginInfo info = new LoginInfo();
		try {
			info.setResult(json.getString(AppsConfig.JSON_RESULT));
			CustomerDao cdao = new CustomerDaoImpl();
			JSONObject data = json.getJSONObject(AppsConfig.JSON_DATA);
			Customer c = cdao.getFromJSON(data);
			info.setData(c);
			info.setMessage(json.getString(AppsConfig.JSON_MESSAGE));
		} catch (Exception e) {
			Log.e("UserBOImpl - getLoginInfo",e.getMessage());
			return null;
		}
		return info;
	}
	@Override
	public JSONResult registerAsync(Customer customer) {
		String url = getRegisterURL(customer);
		JSONObject result = JSONUltil.getJSONObjectFromURL(url);
		if(result != null){
			JSONResult result2 = null;
			try {
				JSONObject registerResult = result.getJSONObject(AppsConfig.JSON_OBJECT);
				if(registerResult != null){
					result2 = getRegisterResult(registerResult);
					return result2;
				}	
				else{
					return null;
				}
			} catch (JSONException e) {
				return null;
			}
		}
		return null;
	}
	public JSONResult getRegisterResult(JSONObject json){
		JSONResult info = new JSONResult();
		try {
			info.setResult(json.getString(AppsConfig.JSON_RESULT));
			info.setMessage(json.getString(AppsConfig.JSON_MESSAGE));
			String code = json.getString(AppsConfig.JSON_DATA);
			info.setData(code);
		} catch (Exception e) {
			Log.e("UserBOImpl - getLoginInfo",e.getMessage());
			return null;
		}
		return info;
	}
	private String getRegisterURL(Customer customer) {
		return AppsConfig.getServiceURL() + "?_module=Customer&_action=register&_view=json"
				+ "&" + customer.getURLQueryString(); 
	}
	private String getActiveURL(String phone, String code) {
		return AppsConfig.getServiceURL() + "?_module=Customer&_action=activecustomer&_view=json"
				+ "&phone=" + phone
				+ "&code=" +code; 
	}
	@Override
	public JSONResult activeCustomerAsync(String phone, String code) {
		String url = getActiveURL(phone, code);
		JSONObject result = JSONUltil.getJSONObjectFromURL(url);
		if(result != null){
			JSONResult result2 = null;
			try {
				JSONObject registerResult = result.getJSONObject(AppsConfig.JSON_OBJECT);
				if(registerResult != null){
					result2 = getActiveResult(registerResult);
					return result2;
				}	
				else{
					return null;
				}
			} catch (JSONException e) {
				return null;
			}
		}
		return null;
	}

	public JSONResult getActiveResult(JSONObject json){
		JSONResult info = new JSONResult();
		try {
			info.setResult(json.getString(AppsConfig.JSON_RESULT));
			info.setMessage(json.getString(AppsConfig.JSON_MESSAGE));
			if(info.getResult().equals(AppsConfig.RESULT_FAIL)){
				String code = json.getString(AppsConfig.JSON_DATA);
				info.setData(code);	
			}
			else{
				JSONObject jsonObject = json.getJSONObject(AppsConfig.JSON_DATA);
				CustomerDao customerDao = new CustomerDaoImpl();
				Customer customer = customerDao.getFromJSON(jsonObject);
				info.setData(customer);
			}
		} catch (Exception e) {
			Log.e("UserBOImpl - getLoginInfo",e.getMessage());
			return null;
		}
		return info;
	}
}
